//app.js
App({
  data:{
    url:"https://eye.heiwangke.net"
  },
  globalData: {
    connected: false, //蓝牙眼镜连接状态
  },
  onLaunch: function () {
    // 展示本地存储能力
    wx.login({
      success:res=>{
        console.log(res)
        this.post('user/wexinLogin',{
          code:res.code
        }).then(res=>{
            wx.setStorageSync('token', res.data.token)
            wx.setStorageSync('nickname', res.data.nickname)
            wx.setStorageSync('mobile', res.data.mobile)
        })
      }
    })
  },
  
  post(url,params){
    return new Promise((resolve,reject)=>{
      wx.request({
        url: this.data.url +'/api/'+ url,
        data: params ? params : {},
        method: "POST",
        header: {
          token: wx.getStorageSync('token') ? wx.getStorageSync('token') : ""
        },
        success: (res) => {
            if (res.data.code == 1) {
              resolve(res.data)
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: "none"
              })
            }
        }
      })
    })
  },

  /**
 * post提交,可返回失败信息
 */
  _post(url, data, success, fail, complete) {
    // 构造请求参数
    data = Object.assign({
      token: wx.getStorageSync('token') ? wx.getStorageSync('token') : ""
    }, data);
 
    wx.request({
      url: this.data.url + '/api/' + url,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
      },
      method: 'POST',
      data,
      success(res) {     
        success && success(res.data);
      },
      fail(res) {     
        fail && fail(res);
      },
      complete(res) {     
        complete && complete(res);
      }
    });
  },

})